﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace SubstringSearcher
{
    class Program
    {
        static void Main(string[] args)
        {
            string folderToSearch = Directory.GetCurrentDirectory() +Path.DirectorySeparatorChar + "SearchHere";
            string outputFile = "results.txt";
            string substringToSearchFor = "sha";

            SubstringSearcher.Model.SubstringSearcher.ReportPrinter printer = (fileName, writer) =>
                {
                    if(fileName.EndsWith(".txt"))
                    {
                        using (StreamReader reader = new StreamReader(fileName))
                        {
                            string firstLineThatContainsTheSubstring = SubstringSearcher.Model.SubstringSearcher.GetFirstLineContaining(reader, substringToSearchFor);

                            if (firstLineThatContainsTheSubstring != null)
                            {
                                //string shortFileName = Path.GetFileName(fileName);
                                writer.WriteLine("{0} - {1}", fileName, firstLineThatContainsTheSubstring);
                            }
                        }
                    }
                };

            using (StreamWriter writer = new StreamWriter(outputFile ))
            {                
                SubstringSearcher.Model.SubstringSearcher.ForeachFileInDirectoryAndSubdirectories(folderToSearch, writer, printer);
            }
        }
    }
}
